<?php

$this->AddHook('download_images', 'DownLoadImages');

function DownLoadImages($bot)
{
	$Name = "ImageDL"; $Version = "1.2"; $Date = "2012-04-28";
	$bot->ld->UpdatePluginVersion($bot, $Name, $Version, $Date );

	$msg = $bot->irc('u') . $bot->irc('b');
	$msg .= $bot->irc('cc') . '2';
	$msg .= $bot->irc('ff') . '13,5' . chr(232) . $bot->irc('ff') . '14,5';
	$msg .= 'Starting Plugin: ImageDL';
	$bot->SendMsg($msg);

	$data = (array) $bot->ld->GetPlSettings("ImageDL");
	$save = "N";

	if (!isset($data['DownloadTime']))
	{
		$data['DownloadTime'] = "";
		$save = "Y";
	}

	if (!isset($data['GameVersion']))
	{
		$data['GameVersion'] = "";
		$save = "Y";
	}

	if (!isset($data['reset']))
	{
		$data['reset'] = "";
		$save = "Y";
	}

	if ($data['reset'] == "10")
	{
		$bot->SendMsg('[ImageDL] Images reset activated.');
		$data['GameVersion'] = "";
		$data['reset'] = "1";
		$save = "Y";
	}

	if ($data['reset'] == "99")
	{
		$bot->SendMsg('[ImageDL] Not all Images were downloaded on last attempt, trying again.');
		$data['GameVersion'] = "";
		$data['reset'] = "1";
		$save = "Y";
	}

	if ($save == "Y")
	{
		$bot->ld->SavePlSettings("ImageDL", $data);
	}

	if ($data['GameVersion'] == $bot->flashRevision)
	{
		$bot->SendMsg('[ImageDL] Images are up to date. ('. $bot->flashRevision .')');
		return;
	}

	$data['GameVersion'] = $bot->flashRevision;
	$data['DownloadTime'] = time();

	$bot->SendMsg('[ImageDL] Checking images started for version: '. $bot->flashRevision);

	$cvServer = 0;
	$FileExistLocal = 0;
	$Filedownload = 0;
	$ImageNotFound = 0;
	$XMLImages = array();
	$UniqueXMLImages = array();

	foreach ($bot->xmlsOb->gsXML->items->item as $item)
	{
		foreach($item->image as $icon)
		{
			if (($icon['name'] == "icon") && ($icon['url']))
			{
				$XMLImages[(string)$item['name']] = $icon['url'];
				$bot->ld->GetOne("REPLACE into Images values ('".(string)$item['name']."', '".$icon['url']."', '', '".$data['GameVersion']."', '')");
			}
		}
	}
	$bot->SendMsg( "[ImageDL] ". count($XMLImages) ." Images In XML.");
	$UniqueXMLImages = array_unique($XMLImages);
	$bot->SendMsg( "[ImageDL] ". count($UniqueXMLImages) ." Unique images In XML.");

	foreach($UniqueXMLImages as $key=>$icon)
	{
		if (file_exists($icon))
		{
			$FileExistLocal++;
			unset($UniqueXMLImages[$key]);
		}
	}
	$bot->SendMsg( "[ImageDL] ". $FileExistLocal ." Images already existed.");
	$bot->SendMsg( "[ImageDL] ". count($UniqueXMLImages) ." Images need to be downloaded.");

	if (count($UniqueXMLImages) == 0)
	{
		$bot->ld->SavePlSettings("ImageDL", $data);
		return;
	}

	$bot->SendMsg( "[ImageDL] Download proccess started. This can take some time when there are many images to get.");

	$bot->ld->ExecQuery("DROP TABLE IF EXISTS 'LatestImages'");
	$bot->ld->ExecQuery("CREATE TABLE [LatestImages] ([ItemName] NVARCHAR(55) NULL, [url] NVARCHAR(100) NULL)");

	foreach($UniqueXMLImages as $key=>$icon)
	{
		$filename = substr($icon, strrpos($icon, "/")+1, strlen($icon));
		$bot->SendMsg("[ImageDL] Downloading: ". $filename ." - ( ". $key ." )");
		$vFolder = substr($icon, 0, strrpos($icon, "/"));

		if (!is_dir($vFolder))
		{
			@mkdir($vFolder, 0777, true);
		}

		$DownloadURL =  "http://cityvillefb". $cvServer .".static.zgncdn.com/static/". $icon;
		$vImageData = file_get_contents($DownloadURL);

		if ($vImageData)
		{
			file_put_contents($icon, $vImageData);
			$Filedownload++;
			$bot->ld->ExecQuery("INSERT INTO LatestImages VALUES ('". $key ."', '". $icon ."')");
		}
		else
		{
			$ImageNotFound++;
		}

		$cvServer++;
		if ($cvServer > 3)
		{
			$cvServer = 0;
		}
	}
	$bot->SendMsg( "[ImageDL] ". $Filedownload ." Images downloaded.");

	if ($ImageNotFound == 0)
	{
		$bot->SendMsg( "[ImageDL] ". $ImageNotFound ." Images not downloaded.");
	}
	else
	{
		$bot->SendMsg( "[ImageDL] ". $ImageNotFound ." Images NOT downloaded, will try next time.");
		$data['reset'] = "99";
	}

	$bot->ld->SavePlSettings("ImageDL", $data);

}

?>